#!/usr/bin/perl

#Ref http://search.cpan.org/~nwclark/perl-5.8.6/pod/perlreftut.pod
#perlreftut - Mark's very short tutorial about references

# 1. Define references
# If you put a \ in front of a variable, you get a reference to that variable.
my @array = [1,2,3];
my %hash = { "a" => 1, "b" => 2};
my $refArray = \@array;
my $refHash = \%hash;

# 2. Two dimension array
my @array1 = (1,2,3);
my @array2 = (4,5,6);
my @array3 = (7,8,9);
my @twoDimArray = ();
push @twoDimArray, \@array1;
push @twoDimArray, \@array2;
push @twoDimArray, \@array3;

printTwoDimArray(@twoDimArray);

sub printTwoDimArray
{
	print "begin--\n";
	my (@array) = @_;
	for ( $i=0; $i < @{array}; $i++)
	{
		my $ref = $array[$i];
		for ( $j=0; $j < @{$ref}; $j++ )
		{
			print $ref->[$j];
			print $array[$i][$j];
		}
	}
	print "end--\n";
}

# 5. Other things
# [ ITEMS ] makes a new, anonymous array, and returns a reference to that array.
# { ITEMS } makes a new, anonymous hash, and returns a reference to that hash.
# but ( ITEMS ) makes a new list/array, and return the list/array itself, not the reference.
my @array1 = (1,2,3);
my $array2 = [1,2,3];
my @array3 = [1,2,3];

# @a is an array with three elements, and each one is a reference to another array.
my @a = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] );

my $i = 0;
my $j = 0;

for ( $i = 0 ; $i < @a ; $i++ )
{
	for ($j = 0 ; $j < @{$a[i]} ; $j++ ) 
	{
		print $a[$i]->[$j];
		print "\n";
	}
}
